<?php
class Admin_Form_ValidatePage{
	
	//CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null){
		//========================================
		// KIEM TRA title
		//========================================
		
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'pages','field'=>'title');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' page_id !=' . $arrParam['page_id'];
			$options = array('table'=>'pages','field'=>'title','exclude'=>$clause);
		}
		$validator = new Zend_Validate();
		
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
	
		if(!$validator->isValid($arrParam['title'])){
			$message = $validator->getMessages();
			$this->_messagesError['title'] = 'Title: ' . current($message);
			$arrParam['title'] = '';
		}		

		//========================================
		// KIEM TRA anh minh hoa
		//========================================
		/* $upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('image');
		$fileName = $fileInfo['image']['name'];
		echo "tenfile".$fileName;
		if(!empty($fileName)){
		
			$upload->addValidator('Extension',true,array('jpg','gif','png'),'image');
			$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'image');
			if(!$upload->isValid('image')){
				$message = $upload->getMessages();
				$this->_messagesError['image'] = 'Avatar: ' . current($message);
				
			}
		} */
		

		if($arrParam['action'] == 'add'){
			$options = array('table'=>'pages','field'=>'image');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' page_id !=' . $arrParam['page_id'];
			$options = array('table'=>'pages','field'=>'image','exclude'=>$clause);
		}
		$validator = new Zend_Validate();
		
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if(!$validator->isValid($arrParam['image'])){
			$message = $validator->getMessages();
			$this->_messagesError['image'] = 'image: ' . current($message);
			$arrParam['image'] = '';
		}
		
		//========================================
		// KIEM TRA main_content
		//========================================
		
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'pages','field'=>'main_content');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' page_id !=' . $arrParam['page_id'];
			$options = array('table'=>'pages','field'=>'main_content','exclude'=>$clause);
		}
		$validator = new Zend_Validate();
		
		
	//	$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if(!$validator->isValid($arrParam['description'])){
			$message = $validator->getMessages();
			$this->_messagesError['description'] = 'description: ' . current($message);
			$arrParam['description'] = '';
		}
		
		
		//========================================
		// KIEM TRA group_id
		//========================================
		//if($arrParam['category_id'] == 0){
	///		$this->_messagesError['type'] = 'Group: Please select a group';
	//	}
		
		
		//========================================
		// KIEM TRA sign
		//========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
				  
		if(!$validator->isValid($arrParam['content'])){
			$message = $validator->getMessages();
			$this->_messagesError['content'] = 'content: ' . current($message);
			$arrParam['content'] = '';
		}	
		
		//========================================
		// TRUYEN CAC GIA TRI DUNG VAO MANG $_arrData
		//========================================
		$this->_arrData = $arrParam;
	
	}
	
	//Kiem tra Error 
	//return true neu co loi xuat hien
	public function isError(){		
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
	}
	
	//Tra ve mot mang cac loi
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	//Tra ve mot mang du lieu sau khi kiem tra
	public function getData($options = null){
		if($options['upload'] == true){
		//	$this->_arrData['image'] = $this->uploadFile();
		}
		return $this->_arrData;
	}
	
	/*========================================
	* 1. Upload user_avatar
	* 2. Resize kich thuoc (100x100 va 450x450)
	* 3. Tra ve ten tap tin upload
	*========================================*/
	 public function uploadFile(){
		//Duong dan den thu muc upload
		$upload_dir = FILES_PATH . '/users';
		
		//========================================
		// UPLOAD FILE $user_avatar
		//========================================
		
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('image');
		$fileName = $fileInfo['image']['name'];
		
		if(!empty($fileName)){
			$fileName = $upload->upload('image', $upload_dir . '/orignal',
							 array('task'=>'rename'),'user_');
			$thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb->resize(400,250)->save($upload_dir . '/img400x250/' . $fileName);

			$thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb->resize(80,50)->save($upload_dir . '/img80x50/' . $fileName);
			
			if($this->_arrData['action'] == 'edit'){
				$upload->removeFile($upload_dir . '/orignal/' . $this->_arrData['current_user_avatar']);
				$upload->removeFile($upload_dir . '/img80x50/' . $this->_arrData['current_user_avatar']);
				$upload->removeFile($upload_dir . '/img400x250/' . $this->_arrData['current_user_avatar']);
			}
		}else{
			if($this->_arrData['action'] == 'edit'){
				$fileName = $this->_arrData['current_user_avatar'];
			}
		}
		
		return $fileName;
	} 
	
	
}